<?php

namespace StudyBuddy\Modules\Blogger;

use \StudyBuddy\Interfaces\Post;
use \StudyBuddy\Interfaces\Question;
use \StudyBuddy\Interfaces\Answer;
use \StudyBuddy\String\HTMLString;

/**
 * Adapter class that takes Post
 * (instance of Question or Answer)
 * as input and returns instance of Entry
 * as output
 *
 */
class BloggerPostAdapter {

    /**
     * Registry object
     *
     * @var object
     */
    protected $oRegistry;

    /**
     * Object Entry
     *
     * @var object
     */
    protected $oEntry;

    /**
     * Constructor
     *
     * @param \StudyBuddy\Registry $o
     */
    public function __construct(\StudyBuddy\Registry $o) {
        $this->oRegistry = $o;
    }

    /**
     * Getter for $this->oEntry
     *
     * @return object of type Entry
     */
    public function getEntry() {
        return $this->oEntry;
    }

    /**
     * Make object Entry
     * from object Post (which can be Answer or Question)
     *
     * @param Post $post object of type Post which
     * can be Answer or Question
     */
    public function makeEntry(Post $o) {
        $this->oEntry = new Entry();
        if ($o instanceof Question) {
            $this->makeQuestionPost($o);
        } elseif ($o instanceof Answer) {
            $this->makeAnswerPost($o);
        }
        d('cp');

        return $this->getEntry();
    }

    /**
     *
     * Setup values in $this->oTumblrPost using
     * values of Question
     *
     * @param Question $o
     */
    protected function makeQuestionPost(Question $o) {
        d('cp');

        /**
         * @todo Translate strings
         *
         * @var string
         */
        $qUrl = $o->getUrl();
        $tpl1 = '<p><a href="%s"><strong>My Question</strong></a> on %s</p>';
        $tpl2 = '<p><a href="%s">Click here</a> to post your reply</p><br>';
        $body = sprintf($tpl1, $qUrl, $this->oRegistry->Ini->SITE_NAME);
        $body .= $o->getBody();
        $body .= sprintf($tpl2, $qUrl);
        $body = HtmlString::factory($body);

        $this->oEntry->setBody($body)->setTitle($o->getTitle());

        $tags = $o['a_tags'];
        d('$tags: ' . print_r($tags, 1));
        if (!empty($tags)) {
            $this->oEntry->addTags($tags);
        }

        return $this;
    }

    /**
     *
     * Setup values in $this->oTumblrPost using
     * values of Answer
     *
     * @param Answer $o
     */
    protected function makeAnswerPost(Answer $o) {
        d('cp');
        $qlink = $this->oRegistry->Ini->SITE_URL . '/q' . $o->getQuestionId() . '/';

        /**
         * @todo Translate string
         *
         * @var string
         */
        $tpl = '<p>This is my answer to a <a href="%s"><strong>Question</strong></a> on %s</p><br>';
        $body = sprintf($tpl, $qlink, $this->oRegistry->Ini->SITE_NAME);

        $body .= $o->getBody();
        d('body: ' . $body);

        $body = HtmlString::factory($body);
        $title = 'My answer to "' . $o->getTitle() . '"';

        $this->oEntry->setBody($body)->setTitle($title);

        return $this;
    }

}
